<template>
  <div class="arr">
    <div class="dashboard-container">
      <div class="app-container">
        <el-card>
          <el-tabs>
            <!-- 放置页签 -->
            <el-tab-pane label="角色管理">
              <!-- 新增角色按钮 -->
              <el-row style="height: 60px">
                <el-button
                  icon="el-icon-plus"
                  size="small"
                  type="primary"
                  @click="isShowDialog = true"
                >新增角色</el-button>
              </el-row>
              <!-- 表格 -->
              <el-table :data="roleList" border="">
                <el-table-column prop="name" label="角色名称" width="240" />
                <el-table-column prop="description" label="描述" />
                <el-table-column label="操作">
                  <template v-slot="scope">
                    <el-button size="small" type="success" @click="assignPerm(scope.row.id)">分配权限</el-button>
                    <el-button
                      size="small"
                      type="primary"
                      @click="editRole(scope.row.id)"
                    >编辑</el-button>
                    <el-button
                      size="small"
                      type="danger"
                      @click="delRole(scope.row.id)"
                    >删除</el-button>
                  </template>
                </el-table-column>
              </el-table>
              <!-- 分页组件 -->
              <el-row
                type="flex"
                justify="center"
                align="middle"
                style="height: 60px"
              >
                <!-- 分页组件 -->
                <!-- <el-pagination layout="prev,pager,next" /> -->
                <!-- 只要有总数和每页长度，自动生成页码 -->
                <el-pagination
                  :total="total"
                  :page-size="pageConfig.pagesize"
                  layout="prev,pager,next"
                  @current-change="currentChange"
                />
              </el-row>
            </el-tab-pane>
            <el-tab-pane label="公司信息">
              <el-form label-width="120px" style="margin-top: 50px">
                <el-form-item label="公司名称">
                  <el-input
                    v-model="companyInfo.name"
                    disabled
                    style="width: 400px"
                  />
                </el-form-item>
                <el-form-item label="公司地址">
                  <el-input
                    v-model="companyInfo.companyAddress"
                    disabled
                    style="width: 400px"
                  />
                </el-form-item>
                <el-form-item label="邮箱">
                  <el-input
                    v-model="companyInfo.mailbox"
                    disabled
                    style="width: 400px"
                  />
                </el-form-item>
                <el-form-item label="备注">
                  <el-input
                    v-model="companyInfo.remarks"
                    type="textarea"
                    :rows="3"
                    disabled
                    style="width: 400px"
                  />
                </el-form-item>
              </el-form>
            </el-tab-pane>
          </el-tabs>
        </el-card>
        <el-dialog
          :title="formData.id ? '编辑角色' : '新增角色'"
          :visible="isShowDialog"
          @close="btnCancel"
        >
          <!-- 校验的三个重点
        1. model 2. rules 3. prop -->
          <el-form ref="addRole" :model="formData" :rules="rules">
            <el-form-item label="角色名称" prop="name">
              <el-input v-model="formData.name" />
            </el-form-item>
            <el-form-item label="描述" prop="description">
              <el-input v-model="formData.description" />
            </el-form-item>
          </el-form>
          <template #footer>
            <el-button
              type="primary"
              size="small"
              @click="btnOK"
            >确定</el-button>
            <el-button size="small" @click="btnCancel">取消</el-button>
          </template>
        </el-dialog>
        <el-dialog title="设置角色权限" :visible="isShowPerm" @close="btnPermCancel">
          <!-- <el-tree
            :data="permList"
            :props="{label: 'name'}"
            default-expand-all
          /> -->
          <el-checkbox-group v-model="permIds">
            <el-tree :data="permList">
              <template v-slot="scope">
                <el-checkbox :label="scope.data.id">{{ scope.data.name }}</el-checkbox>
              </template>
            </el-tree>
          </el-checkbox-group>
          <template #footer>
            <el-button size="small" type="primary" @click="btnPermOK">确定</el-button>
            <el-button size="small" @click="btnPermCancel">取消</el-button>
          </template>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getCompanyInfo,
  getRoleList,
  addRole,
  getRoleDetail,
  editRole,
  delRole,
  assignPerm
} from '@/api/setting'
import { getPermList } from '@/api/permission'
import { listToTreeData } from '@/utils'
export default {
  data() {
    return {
      formData: {
        // 表单字段取决于后台设计
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '该项不能为空', trigger: 'blur' }],
        description: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ]
      },
      isShowPerm: false,
      // 所有权限列表
      permList: [],
      // 被选中的权限id数组
      permIds: [],
      roleId: '',
      companyInfo: {},
      pageConfig: {
        page: 1,
        pagesize: 2
      },
      roleList: [],
      total: 0,
      isShowDialog: false
    }
  },
  async created() {
    this.companyInfo = await getCompanyInfo(this.$store.getters.companyId)
    console.log(76, this.companyInfo)

    // 将获取角色列表功能封装成函数,页码进入时调用一次
    this.getRoleList()
    // 回显设置角色权限弹窗的所有权限列表
    this.permList = listToTreeData(await getPermList(), '0')
  },
  methods: {
    async assignPerm(roleId) {
      // 分配角色权限, 其实是在编辑角色
      const res = await getRoleDetail(roleId)
      console.log('角色详情', res)
      this.permIds = res.permIds
      // 需要先获取角色详情
      // 记录被点击的角色id
      // 方便确定按钮请求
      this.roleId = roleId
      this.isShowPerm = true
    },
    async btnPermOK() {
      // 发送请求
      await assignPerm({
        id: this.roleId,
        permIds: this.permIds
      })
      // 提示用户
      this.$message.success('操作成功')
      // 关闭弹窗
      this.isShowPerm = false
    },
    btnPermCancel() {
      // 清理数据
      this.permIds = []
      // 关闭弹窗
      this.isShowPerm = false
    },
    currentChange(newPage) {
      console.log(newPage)
      this.pageConfig.page = newPage
      // 将获取角色列表功能封装成函数, 每次翻页都调用一下
      this.getRoleList()
    },
    async getRoleList() {
      // 发送请求获取角色列表, 带上页码设定
      const { rows, total } = await getRoleList(this.pageConfig)
      this.roleList = rows
      this.total = total
    },
    async editRole(id) {
      // 点击编辑的时候就会触发, 可以获取到当前被点击的 id
      // 1. 根据 id 获取详情回显
      this.formData = await getRoleDetail(id)
      console.log(id)
      // 2.弹出弹窗
      this.isShowDialog = true
    },
    async delRole(id) {
      try {
        console.log(id)
        // 二次询问
        await this.$confirm('是否确认删除？')
        // 发请求带上id
        await delRole(id)
        // 提示用户
        this.$message.success('删除成功')

        if (this.roleList.length === 1 && this.pageConfig.page > 1) {
          this.pageConfig.page -= 1
        }
      } catch (error) {
        console.log(error)
      }
    },
    async btnOK() {
      try {
        // 校验表单
        await this.$refs.addRole.validate()
        // 发请求
        // 如果 this.formData 有 id 就是编辑否则是新增
        if (this.formData.id) {
          // 编辑
          await editRole(this.formData)
        } else {
          // 新增
          await addRole(this.formData)
        }
        // 关闭弹窗
        this.isShowDialog = false
        // 提示用户
        this.$message.success('操作成功')
        // 更新页面
        this.getRoleList()
      } catch (error) {
        console.log(error) + 3
      }
    },
    btnCancel() {
      // 清理表单
      this.formData = {
        name: '',
        description: ''
        // 这个清理函数可以清理绑定过的默认值
        // 和报错的提示
        // 额外追加的数据则无法清理
      }
      this.$refs.addRole.resetFields()
      // 关闭弹窗
      this.isShowDialog = false
    }
  }
}
</script>

<style>
.arr {
  margin: 30px;
}
</style>
